package com.gpy.hospital.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.dao.BlogMapper;
import com.gpy.hospital.cms.pojo.Blog;
import com.gpy.hospital.cms.service.IBlogService;
import com.gpy.hospital.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogServiceImpl implements IBlogService {
    @Autowired
    private BlogMapper blogMapper;
    /**
     * 带有分页查询所有文章信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Blog> getAll(Integer pageNum, Integer pageSize, String blogName) {
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogs = blogMapper.getAll(blogName);
        PageInfo pageInfo = new PageInfo(blogs);
        return pageInfo;
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @Override
    public Blog getById(Integer id) {
        return blogMapper.selectByPrimaryKey(id);
    }


    /**
     * 添加文章
     * @param blog
     * @return
     */
    @Override
    public ServerResponse addArticle(Blog blog) {
        int result = 0;
        if (blog.getId() == null) {
            result = blogMapper.insertSelective(blog);
        } else {
            result = blogMapper.updateByPrimaryKeySelective(blog);
        }
        if (result > 0) {
            return ServerResponse.createByErrorMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }

    /**
     * 根据文章id删除
     * @param id
     */
    @Override
    public ServerResponse deleteById(Integer id) {
        int result = blogMapper.deleteByPrimaryKey(id);
        if (result > 0) {
            return ServerResponse.createByErrorMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public List<Blog> getRandom() {
        return blogMapper.getRandom();
    }

    @Override
    public List<Blog> getAllByStatus() {
        return blogMapper.getAllByStatus();
    }

    @Override
    public List<Blog> getAllByTitle(String searchWords) {
        return blogMapper.getAllByTitle(searchWords);
    }
}
